package java21demo;

record Address(String city, String street, String zip){}
record Person(String name, int age, Address address) {
};

public class RecordPatternMain {
	public static void main(String[] args) {
		Person p = new Person("小王", 19, new Address("厦门","集美", "361021"));

		/* 演示1：Record Patter */
		// java 16 之前。
		/*if (p instanceof Person person) {
			System.out.println("姓名 = "+ person.name() + ", 年龄 = "+ person.age());
		}*/

		// java 21。将Record类型的p1分解成name与age
		if (p instanceof Person(String name, int age, Address address)) {
			System.out.println("姓名 = " + name + ", 年龄 = "+ age);
			System.out.println("城市 = " + address.city());
		}

		/* 2. Nested Pattern*/
		// 将Record类型的p2分解成name, age
		// 进一步将嵌套在Person中的Record类型Address分解成c,s,z
		if (p instanceof Person(String name, int age, Address(String c, String s, String z))){
			System.out.println("姓名 = " + name + ", 年龄 = "+ age);
			System.out.println("城市 = " + c);
		}
	}
}
